#include "gd32f30x.h"   
#include "FreeRTOS.h"
#include "task.h"
#include "event_groups.h"
#include "queue.h"
#include "semphr.h"
#include "BMP280.h"
#include "BMP280_Register.h"
#include "ui.h"
extern EventGroupHandle_t EventGroup_Handle;
extern TaskHandle_t LEDTask_Handler;
extern QueueHandle_t myqueue_Handle;
extern SemaphoreHandle_t Mutex_Handle; 
uint16_t rbuf[3];
uint32_t value;
BaseType_t rret;
uint8_t light;
uint8_t key_num;
void LED_init(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);
	gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);




}
/**
 * @brief 获取LED任务的状态，如果不是suspend状态则把任务挂起，如果任务是suspend状态，则resume恢复任务
 * 
 * @param 无
 */
void LED_Suspend_Task(void *pa)
{
	for(;;)
	{
		EventBits_t evbits = xEventGroupWaitBits(EventGroup_Handle, 0x03, pdTRUE, pdFALSE, pdMS_TO_TICKS(20));
		if((evbits & 0x03) == 0x03)
		{
			eTaskState ret = eTaskGetState(LEDTask_Handler);	
			(ret == eSuspended) ? vTaskResume(LEDTask_Handler) : vTaskSuspend(LEDTask_Handler);
			key_num %= 254;
			key_num++;
		}
		change_mainpage();
		vTaskDelay(20);
	}
}

void LED_Task(void *para)
{
	for(;;)
	{
		GPIO_BC(GPIOB) = GPIO_PIN_2;
		vTaskDelay(500);
		GPIO_BOP(GPIOB) = GPIO_PIN_2;
		vTaskDelay(500);
	}
}
void ADC_Get_LED_Task(void *parameters)
{
	for(;;)
	{
		value = 0;
		for(uint8_t i = 0; i<3; i++)
		{
			rret = xQueueReceive(myqueue_Handle, &rbuf[i], pdMS_TO_TICKS(50));		
		}
		value = rbuf[0] + rbuf[1] + rbuf[2];
		value/=3;
		light = (float)value/4095*100;
		
		timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, light);
		vTaskDelay(5);
	}
}
